//
// Created on 2024/6/26.
//
/*
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
*/

#ifndef DOUBLE_QUEUE_SURFACE_MODE_VIDEO_ENCODER_OHOSEGLCONTEXTMANAGE_H
#define DOUBLE_QUEUE_SURFACE_MODE_VIDEO_ENCODER_OHOSEGLCONTEXTMANAGE_H

#include <mutex>
#include <queue>

#include "GLES3/gl32.h"
#include "EGL/egl.h"

namespace webrtc {
namespace ohos {

class OhosEGLContextManage {
public:
    static OhosEGLContextManage& GetInstance() { return ohosEGLContextManage_; }
    GLuint PopTextureID();
    int32_t PushTextureID(GLuint textureID);
    bool TextureIDQueueEmpty();
    std::mutex& GetMutex() { return textureIDMutex_; }
    std::condition_variable& GetConditionVariable() { return textureIDCond_; }
    bool SetEGLContext(EGLContext eglContext) { eglContext_ = eglContext; return true; }
    EGLContext& GetEGLContext() { return eglContext_; }

    static uint16_t GetDesktopCnt() { return desktopCnt_;}
    static uint16_t GetDrawCnt() { return drawCnt_; }
    static uint16_t GetOutputCnt() { return outputCnt_; }
    static void AddDesktopCnt() { ++desktopCnt_;}
    static void AddDrawCnt() { ++drawCnt_; }
    static void AddOutputCnt() { ++outputCnt_; }
    static uint16_t desktopCnt_;
    static uint16_t drawCnt_;
    static uint16_t outputCnt_;
private:
    OhosEGLContextManage() {};
    ~OhosEGLContextManage() {};
    OhosEGLContextManage(OhosEGLContextManage&) = delete;
    OhosEGLContextManage& operator=(const  OhosEGLContextManage&) = delete;
    static OhosEGLContextManage ohosEGLContextManage_;

    std::mutex textureIDMutex_;
    std::condition_variable textureIDCond_;
    EGLContext eglContext_ {EGL_NO_CONTEXT};

    // textureID队列和相关的mutex condition_variable
    std::mutex queueMutex_;
    std::condition_variable queueCond_;
    std::queue<GLuint> textureIDQueue_;
};

} // namespace ohos
} // namespace webrtc

#endif //DOUBLE_QUEUE_SURFACE_MODE_VIDEO_ENCODER_OHOSEGLCONTEXTMANAGE_H
